<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Menu_model extends CI_Model {
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database(); 
    }
    
    function showallmenu(){
    	$this->load->database();   
		$this->db->order_by("menuid", "desc");
		$query = $this->db->get('tblmenus');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
    }
    
    function getdetailmenu($id){
    	$this->load->database();   
		$this->db->where('menuid',$id);
		$query = $this->db->get('tblmenus');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
    }
    
   
    
    function getmenurootbychild($id=null){
   	 $this->load->database();   
		$this->db->where('menuid',$id);
		$query = $this->db->get('tblmenus');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
    }
    function showmenubyorder($order=0){
   	 $this->load->database();   
   	 	if($order == 1){
			$this->db->order_by("menuorder", "desc");
   	 	}elseif($order == 2){
   	 		$this->db->order_by("menuorder", "asc");
   	 	}else{
   	 		
   	 	}
		$query = $this->db->get('tblmenus');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
    }
    
function showmenubyorderbyroot($order=0){
   	 $this->load->database();   
   	 	if($order == 1){
			$this->db->order_by("menuorder", "desc");
   	 	}elseif($order == 2){
   	 		$this->db->order_by("menuorder", "asc");
   	 	}else{
   	 		
   	 	}
		$query = $this->db->get('tblmenus');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
    }
    
    /*
     * Typemenu == 1 : là menu hiển thị trên trang chủ
     *  Typemenu = 0: là category;
     */
	function _addmenu($menuroot,$menuname,$menutext,$menuorder,$menuactive){
    	$this->load->database();
    	$data = array( 
            'menuroot'=>$menuroot,
            'menuname'=>$menuname,
    		'menutext'=>$menutext,
    		'menuorder'=>$menuorder,
    		'menuactive'=>$menuactive,
    		'typemenu' =>1
            );
    	$this->db->insert('tblmenus', $data); 
    }
    
	function _edit_menu($menuid,$menuroot,$menuname,$menutext,$menuorder,$menuactive){ 
		$data = array(  
            'menuroot'=>$menuroot,
            'menuname'=>$menuname,
    		'menutext'=>$menutext,
    		'menuorder'=>$menuorder,
    		'menuactive'=>$menuactive,
			'typemenu' =>1
		); 
		$this->db->where('menuid', $menuid);
		$this->db->update('tblmenus', $data);
	}
	
	function deletemenu($id){
		$this->load->database();
		$this->db->where('menuid', $id);
		$this->db->delete('tblmenus');
	}
    
    
}